<?php

namespace App\Kuafu\Shop\Controller\Merchant;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Common\Service\CommonService;
use App\Kuafu\Config\Exceptions\ConfigException;
use App\Kuafu\Config\Service\ConfigService;
use App\Kuafu\Shop\Exceptions\ShopException;
use App\Kuafu\Shop\Service\ToolsService;
use App\Kuafu\User\Exceptions\UserException;
use App\Kuafu\User\Service\AuthService;
use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
use Endroid\QrCode\Label\Alignment\LabelAlignmentCenter;
use Endroid\QrCode\Label\Font\NotoSans;
use Endroid\QrCode\Label\Label;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * APP 工具箱
 */
class ToolsController extends BaseController
{

    /**
     * 外卖好评卡样例图
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ConfigException
     */
    public function getWaiMaiPicDemo(Request $request): JsonResponse
    {
        $card         = ConfigService::listByTypeAndKey('praiseCard', 'praiseCardConfig');
        $demoCardInfo = json_decode($card['praiseCardConfig'] ?? '', true);
        return $this->success([
            'lists' => collect($demoCardInfo)->pluck("demo")->toArray()
        ]);
    }


    /**
     * 生成好评卡
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ConfigException
     * @throws UserException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function genWaiMaiPic(Request $request): JsonResponse
    {
        $price     = (int)$request->get('price');
        $tel       = $request->get('tel');
        $wechatUrl = $request->get('wechatUrl');
        $index     = (int)$request->get('index', 0);

        if (strlen($tel) == 11 && strpos($tel, '1') === 0) {
            $tel = substr($tel, 0, 3) . '-' . substr($tel, 3, 4) . '-' . substr($tel, 7, 4);
        }

        return $this->success(ToolsService::generatePraiseCard($price, $tel, $wechatUrl, $index));
    }


    /**
     * 物料素材模版列表
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function list(Request $request): JsonResponse
    {
        $type = (int)$request->get('type');
        return $this->success(ToolsService::list($type));
    }

    /**
     * 根据选择物料素材模版id创建
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ShopException
     * @throws UserException
     */
    public function create(Request $request, int $id): JsonResponse
    {
        $shopId                  = $request->header("shopId");
        $userId                  = AuthService::getUserId();
        $price                   = (int)$request->get('price', 0);
        $num                     = (int)$request->get('num', 0);
        $tel                     = (string)$request->get('tel', '');
        $wechatUrl               = (string)$request->get('wechatUrl', '');
        $recruitmentTitle        = (string)$request->get('recruitmentTitle', '');
        $recruitmentRequire      = (string)$request->get('recruitmentRequire', '');
        $recruitmentTitleRight   = (string)$request->get('recruitmentTitleRight', '');
        $recruitmentRequireRight = (string)$request->get('recruitmentRequireRight', '');
        $recruitmentExtra        = (string)$request->get('recruitmentExtra', '');
        $address                 = (string)$request->get('address', '');
        $data                    = (array)$request->all();
        if ($tel) {
            if (strlen($tel) == 11 && strpos($tel, '1') === 0) {
                $tel = substr($tel, 0, 3) . '-' . substr($tel, 3, 4) . '-' . substr($tel, 7, 4);
            }
        }
        $recruitmentExtra = '';
        return $this->success(ToolsService::create(
                $shopId, $id, $userId, $price, $num, $tel, $wechatUrl, $address, $recruitmentTitle, $recruitmentRequire,
                $recruitmentExtra, $recruitmentTitleRight, $recruitmentRequireRight, $data
            ));
    }

    /**
     * 物料素材模版单个详细
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function detail(Request $request, int $id): JsonResponse
    {
        return $this->success(ToolsService::detail($id));
    }


    /**
     * 物料素材模版配置
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function typeConfig(Request $request): JsonResponse
    {
        return $this->success(ToolsService::typeConfig());
    }
}
